package servlet;

import dao.AddressDao;
import dao.GoodsDao;
import dao.TypeDao;
import entity.Address;
import entity.Goods;
import entity.Type;
import entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/sortGoods")
public class SortGoodsServlet extends HttpServlet {
	
	@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        doGet(request, response);
    }
	@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {

		System.out.println("-----------sortGoods------------");
		String typeId = request.getParameter("typeId");
		GoodsDao goodsDao = new GoodsDao();
		if (typeId != null && !typeId.isEmpty()) {
			List<Goods> ByTypesGoods = goodsDao.selectGoodsByType(Integer.valueOf(typeId));
			if(ByTypesGoods == null) {
				response.sendRedirect("error.jsp");
				return;
			}else {
				request.getSession().setAttribute("ByTypesGoods",ByTypesGoods);
				response.sendRedirect("frontend/sortgoods.jsp");
			}
		}else {
			List<Goods> ByTypesGoods = goodsDao.selectAllGoods();
			if(ByTypesGoods == null) {
				response.sendRedirect("error.jsp");
				return;
			}else {
				request.getSession().setAttribute("ByTypesGoods",ByTypesGoods);
				response.sendRedirect("frontend/sortgoods.jsp");
			}
		}
	}


}
